//
//  HomeTableViewController.swift
//  swift_wobo
//
//  Created by yuxin on 2018/11/13.
//  Copyright © 2018 wuxiantianli. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        visitorView?.setupVisitorInfo(isHome: true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
        
       setupNavgationItem()
        
        NotificationCenter.default.addObserver(self, selector: #selector(popoverViewWillChangeNoti(noti:)), name: NSNotification.Name(rawValue: YXPopoverAnimatorWillChange), object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - 懒加载
    /// 自定义转场对象
    private lazy var popoverAnimator : PopoverAnimator = {
       let pa = PopoverAnimator()
        pa.presentFrame = CGRect(x: 100, y: 56, width: 250, height: 300)
        
        return pa
    }()

}


// MARK: - UI
extension HomeTableViewController {
    
    private func setupNavgationItem() {
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(letBtnClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBtnClick))
        
        let titleBtn = TitleButton()
        titleBtn.setTitle("阿斯兰可汗", for: .normal)
        titleBtn.sizeToFit()
        titleBtn.addTarget(self, action: #selector(titleBtnClick(btn:)), for: .touchUpInside)
        navigationItem.titleView = titleBtn
        
    }
    
}

// MARK: - action
extension HomeTableViewController {
    @objc private func letBtnClick() {
       print(#function)
    }
    @objc private func rightBtnClick() {
        let codeVC = QRCodeViewController()
        let navi = UINavigationController(rootViewController: codeVC)
        navi.navigationBar.barTintColor = UIColor.black
        present(navi, animated: true, completion: nil)
    }
    @objc private func titleBtnClick(btn:TitleButton) {
        btn.isSelected = !btn.isSelected
        
        let vc = PopoverViewController()
        vc.transitioningDelegate = popoverAnimator
        vc.modalPresentationStyle = .custom
        present(vc, animated: true, completion: nil)
        
    }
    @objc private func popoverViewWillChangeNoti(noti : Notification) {
        guard let userInfo = noti.userInfo else {
            return
        }
        
        let presentedStatus : String = userInfo[YXPopoverViewStatuskey] as! String
        let titleBtn = navigationItem.titleView as! TitleButton
       
        if presentedStatus == "show" {
            titleBtn.isSelected = true
        }else if presentedStatus == "dismiss" {
            titleBtn.isSelected = false
        }
        
    }
    
}
